/* Andreas Baum
 * IMClientApp.java
 * Handles the connection of an IM client to the IM server
 * CS 78 - Networks - Programming Assignment 1
 */


package jbpClientTester;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;




public class JBPClientTester {

	public static int SERVER_PORT = 4444;
	public static int I_TUNES_PORT = 3689;

	private static Socket socket;
	private static PrintWriter out;
	private static BufferedReader in;
	private static String host = "andreas-baum.kiewit.dartmouth.edu";
	
	public JBPClientTester() {
		out = null;
		in = null;
	}

	public static void main(String args[]) throws IOException {
		socket = null;

		System.out.println("\t*** Welcome to the Bonjour Proxy Client! ***");

		System.out.println("Attempting to connect to " + host + "...");

		try {
			// send connection request:
			socket = new Socket(host, SERVER_PORT);
			// establish in and out streams
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about host: " + host + ".");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".");
			System.exit(1);
		}

		// Confirm connection:
		System.out.println("Connected to " + host 
				+ " on port " + SERVER_PORT + ".");


		// connection is established, hand it over to a client thread.
		DataThread extThread = new DataThread(socket, "extThread");
		Socket iTunesSocket = new Socket("localhost", I_TUNES_PORT);
		DataThread iTunesDataThread = new DataThread(iTunesSocket, "iTunesDataThread");
	
		extThread.setOtherThread(iTunesDataThread);
		iTunesDataThread.setOtherThread(extThread);
		System.out.println("READY and waiting.");
		
		out.close();
		in.close();

	}

}